package com.tizona.dao.jdbc;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Vector;

public class FastStatement
{

    public FastStatement(PreparedStatement preparedstatement, String s)
    {
        pstmt = preparedstatement;
        sql = s;
        params = new Vector();
        params.setSize(18);
    }

    public String getSQLString()
    {
        StringBuffer stringbuffer = new StringBuffer("FAST: [");
        int i = 1;
        for(int j = 0; j < sql.length(); j++)
        {
            char c = sql.charAt(j);
            if(c == '?')
                stringbuffer.append((String)params.get(i++));
            else
                stringbuffer.append(c);
        }

        stringbuffer.append("]");
        return stringbuffer.toString();
    }

    public void setString(int i, String s)
        throws SQLException
    {
        pstmt.setString(i, s);
        params.set(i, s);
    }

    public void setBigDecimal(int i, BigDecimal bigdecimal)
        throws SQLException
    {
        pstmt.setBigDecimal(i, bigdecimal);
        params.set(i, bigdecimal.toString());
    }

    public void setNull(int i, int j)
        throws SQLException
    {
        pstmt.setNull(i, j);
        params.set(i, new String("NULL"));
    }

    public void setInt(int i, int j)
        throws SQLException
    {
        pstmt.setInt(i, j);
        params.set(i, (new Integer(j)).toString());
    }

    public void setLong(int i, long j)
        throws SQLException
    {
        pstmt.setLong(i, j);
        params.set(i, (new Long(j)).toString());
    }

    public void setFloat(int i, float f)
        throws SQLException
    {
        pstmt.setFloat(i, f); 
        params.set(i, (new Float(f)).toString());
    }

    public void setDouble(int i, double f)
        throws SQLException
    {
        pstmt.setDouble(i, f);
        params.set(i, (new Double(f)).toString());
    }

    public void setBoolean(int i, boolean flag)
        throws SQLException
    {
        pstmt.setBoolean(i, flag);
        params.set(i, flag ? "1" : "0");
    }

    public void setDate(int i, java.sql.Date x)
        throws SQLException
    {
        pstmt.setDate(i, x);
        params.set(i, x.toString());
    }

    public void setTime(int i, java.sql.Time x)
        throws SQLException
    {
        pstmt.setTime(i, x);
        params.set(i, x.toString());
    }

    public void setTimestamp(int i, java.sql.Timestamp x)
        throws SQLException
    {
        pstmt.setTimestamp(i, x);
        params.set(i, x.toString());
    }


    public int update()
        throws SQLException
    {
        pstmt.clearWarnings();
        return pstmt.executeUpdate();
    }

    public ResultSet query()
        throws SQLException
    {
        ResultSet query = pstmt.executeQuery();
        pstmt.clearWarnings();
        return query; 
    }

    public void close()
            throws SQLException
    {
        pstmt.clearWarnings();
        pstmt.close();
    }

    PreparedStatement pstmt;
    String sql;
    Vector params;
}
